(function (window, $, undefined) {
  'use strict';

  var dictData = {
    status: {

    },
    cityId: {

    }
  };

  var trainingData = {};

  main(function () {

    var $tableTraining = $('#table-training');
    util.jqGrid($tableTraining, {
      url: ajaxURL.ORGANIZATION + '/driverTraining/queryWithPage',
      // datatype: 'json',
      autowidth: true,
      height: $(window).height() - 270,
      multiselect: true,
      multiboxonly: true,
      // shrinkToFit: false,
      colModel: [
        { name: 'kid', key: true, hidden: true },
        { label: '驾驶员培训课程名称', name: 'courseName', width: 120 },
        {
          label: '培训课程日期', name: 'courseDate', width: 100, formatter: function(v) {
          return util.getDate(v);
        }
        },
        {
          label: '注册地', name: 'address', width: 80, formatter: function (v) {
            return dictData.cityId[v] || '';
          }
        },
        { label: '培训时长', name: 'duration', width: 60 },
        { label: '机动车驾驶证号', name: 'licenseId', width: 80 },
        {
          label: '培训开始时间', name: 'startTime', width: 80, formatter: function (v) {
            return util.getDate(v);
          }
        },
        {
          label: '培训结束时间', name: 'stopTime', width: 80, formatter: function (v) {
            return util.getDate(v);
          }
        },
        {
          label: '录入时间', name: 'createTime', width: 80, formatter: function (v, o, r) {
            return util.getDate(v);
          }
        }
      ],
      loadComplete: function (res) {
        trainingData = {};
        if (res && res.data) {
          $.each(res.data, function (_, item) {
            trainingData[item.kid] = item;
          });
        }
      },
      pager: '#pager-training'
    });

    // 获取城市列表
    util.get(ajaxURL.ORGANIZATION + '/city/getCityList?pageNum=1&pageSize=99999', function (res) {
      util.selectHtml($('select[name="address"]'), res.data, 'cityId', 'name', '请选择');
      $.each(res.data, function (_, item) {
        dictData.cityId[item.cityId] = item.name;
      });
      searchTable();
    });

    util.getDict('status', 'status').then(function (data) {
      util.selectHtml('[name="status"]', data, 'value', 'name', '请选择');
      $.each(data, function (_, item) {
        dictData.status[item.value] = item.name;
      });
    });

    function searchTable() {
      util.searchGrid($tableTraining);
    }

    var $formTraining = $('#form-training');
    var $layerTraining = $('#layer-training');
    var $validateTraining = $formTraining.validate();

    // 时间选择
    laydate.render({
      elem: '#courseDate'
    });
    laydate.render({
      elem: '#trainingTime',
      range: '至'
    });

    $('#btn-add').click(function () {
      util.resetForm($formTraining);
      util.layerOpen2({
        title: '新增驾驶员培训信息',
        content: $layerTraining,
        area: ['800px', ''],
        btn: ['新增', '取消'],
        yes: function (index) {
          if ($validateTraining.form()) {
            var data = util.formData($formTraining);
            var postData = {
              address: data.address,
              courseDate: util.getTime(data.courseDate),
              courseName: data.courseName,
              duration: data.duration,
              licenseId: data.licenseId
            };

            var temp = data.trainingTime.split('至');
            postData.startTime = util.getTime($.trim(temp[0]));
            postData.stopTime = util.getTime($.trim(temp[1]));

            layer.confirm('确认提交当前数据？', function () {
              util.post(ajaxURL.ORGANIZATION + '/driverTraining/addDriverTraining', postData, function (res) {
                layer.close(index);
                util.tip.success('新增成功');
                $tableTraining.trigger('reloadGrid');
              });
            });

          }
        }
      })
    });

    // 查看详情
    $('#btn-view').click(util.oneCheck($tableTraining, function (id, data) {
      var curData = trainingData[id];
      util.resetForm($formTraining);

      curData.courseDate = util.getDate(curData.courseDate);
      curData.trainingTime = util.getDate(data.startTime) + ' 至 ' + util.getDate(data.stopTime);

      util.fillForm($formTraining, curData);
      util.layerOpen({
        title: '查看驾驶员培训信息',
        content: $layerTraining,
        area: ['800px', '']
      });
    }));



  });


})(window, jQuery);
